import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
// 前台状态管理
function sessionStg(state) {
  window.sessionStorage.setItem("state", JSON.stringify(state));
}
export default new Vuex.Store({
  state: {
    title: "", // 标签页title
    isLogin: false, // 是否登录
    currentRouterName: "home", // 当前的Router名,
    activeHeaderMenu: ["home"], // header menu菜单
    account: "", // 用户名
    mobile: "", // 手机号
    isSign: false, // 是否签订电子协议
    isValidate: false, // 是否实名验证
    isMobile: false, // 是否手机认证，
    isSetPay: false, //是否设置支付密码
    settingTab: "1", // settingTab的key
    isShowNav: true, // 是否显示底部导航栏, 移动端
    searchPageRouter: "", // 搜索页面是哪一个页面进来的
    invite: null, // 邀请码
    memberType: "" //金融类型
  },
  actions: {
    resetState(context) {
      context.commit("resetState");
    },
    changeMemberType(context, info) {
      context.commit("changeMemberType", info);
    },
    changeIsShowNav(context, isShowNav) {
      context.commit("changeIsShowNav", isShowNav);
    },
    changeInvite(context, info) {
      context.commit("changeInvite", info);
    },
    changeTitle(context, title) {
      context.commit("changeTitle", title);
    },
    changeLoginStatus(context, login) {
      context.commit("changeLoginStatus", login);
    },
    changeCurrentRouter(context, routerName) {
      context.commit("changeCurrentRouter", routerName);
    },
    changeActiveMenu(context, routerName) {
      context.commit("changeActiveMenu", routerName);
    },
    changeAccount(context, info) {
      context.commit("changeAccount", info);
    },
    changeMobile(context, info) {
      context.commit("changeMobile", info);
    },
    changeValidate(context, info) {
      context.commit("changeValidate", info);
    },
    changeSign(context, info) {
      context.commit("changeSign", info);
    },
    changeMobileStatus(context, info) {
      context.commit("changeMobileStatus", info);
    },
    changeSetPay(context, info) {
      context.commit("changeSetPay", info);
    },
    changeSettingTab(context, info) {
      context.commit("changeSettingTab", info);
    },
    changeSearchPageRouter(context, info) {
      context.commit("changeSearchPageRouter", info);
    }
  },
  mutations: {
    resetState(state) {
      let obj = {
        title: "", // 标签页title
        isLogin: false, // 是否登录
        account: "", // 用户名
        mobile: "", // 手机号
        isSign: false, // 是否签订电子协议
        isValidate: false, // 是否实名验证
        isMobile: false, // 是否手机认证，
        isSetPay: false, //是否设置支付密码
        settingTab: "1", // settingTab的key
        memberType: ""
      };
      Object.assign(state, obj);
      sessionStg(state);
    },
    changeMemberType(state, info) {
      state.memberType = info;
      sessionStg(state);
    },
    changeIsShowNav(state, isShowNav) {
      state.isShowNav = isShowNav;
      sessionStg(state);
    },
    changeInvite(state, info) {
      state.invite = info;
      sessionStg(state);
    },
    changeTitle(state, title) {
      state.title = title + "-好融艺";
      window.document.title = state.title;
      sessionStg(state);
    },
    changeLoginStatus(state, login) {
      state.isLogin = login;
      sessionStg(state);
    },
    changeCurrentRouter(state, routerName) {
      state.currentRouterName = routerName;
      sessionStg(state);
    },
    changeActiveMenu(state, routerName) {
      state.activeHeaderMenu = [routerName];
      sessionStg(state);
    },
    changeAccount(state, info) {
      state.account = info;
      sessionStg(state);
    },
    changeMobile(state, info) {
      state.mobile = info;
      sessionStg(state);
    },
    changeValidate(state, info) {
      let validate = null;
      if (info === 1) {
        validate = true;
      } else {
        validate = false;
      }
      state.isValidate = validate;
      sessionStg(state);
    },
    changeMobileStatus(state, info) {
      let validate = null;
      if (info === 1) {
        validate = true;
      } else {
        validate = false;
      }
      state.isMobile = validate;
      sessionStg(state);
    },
    changeSetPay(state, info) {
      let validate = null;
      if (info === 1) {
        validate = true;
      } else {
        validate = false;
      }
      state.isSetPay = validate;
      sessionStg(state);
    },
    changeSign(state, info) {
      let validate = null;
      if (info === 2) {
        validate = true;
      } else {
        validate = false;
      }
      state.isSign = validate;
      sessionStg(state);
    },
    changeSettingTab(state, info) {
      state.settingTab = info;
      sessionStg(state);
    },
    changeSearchPageRouter(state, info) {
      state.searchPageRouter = info;
      sessionStg(state);
    }
  }
});
